import time
from selenium import webdriver
from selenium.webdriver.common.by import By    #获取该文件全部元素

import pytest

import random

from selenium.webdriver.support.ui import WebDriverWait    #显性时间
from selenium.webdriver.support import expected_conditions as EC  #显性时间

import allure


@allure.feature("不二掌柜网站")
class Test_ber:              #a=self.driver

    @allure.story("测试用例1：退出")
    def test_exit(self,a):
        time.sleep(1)
        with allure.step("点击个人头像"):
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[text()='老板']"))).click()   #点击老板
        with allure.step("点击退出"):
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[text()=' 退出登录 ']"))).click()   #点击退出登录
        time.sleep(1)

        with allure.step("退出断言"):
            a1 = a.find_elements(By.CLASS_NAME, "form-container-header-title")[0].text
            assert a1 == "登录"
            print("登出断言成功")


    @allure.story("测试用例2：销售预定新增")
    def test_add(self,a):
        with allure.step("点击销售"):
            time.sleep(1)
            WebDriverWait(a, 10, 0.5).until(EC.presence_of_element_located((By.XPATH, "//*[text()='销售']"))).click()
        time.sleep(1)
        with allure.step("点击销售预订"):
            WebDriverWait(a, 10, 0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "text")))[1].click()
        with allure.step("进入销售预订页面断言"):
            time.sleep(1)
            a1 = a.find_elements(By.CLASS_NAME, "tags-view-item")[1].text
            assert a1 == "销售预订"

        with allure.step("新增"):
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[text()=' 新增 ']"))).click()  # 新增
        with allure.step("客户"):
            time.sleep(4)
            WebDriverWait(a,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"el-input__inner")))[3].click()  # 客户
        with allure.step("客户（自定选杨田田那条）"):
            time.sleep(3)
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[text()='杨甜甜']"))).click()   #客户（自定选杨田田那条）
        with allure.step("仓库"):
            time.sleep(3)
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[@id='app']/div/div[2]/section/div[2]/form/div[2]/div/div/div/div/div[2]/div[2]/div/div[2]/table/tbody/tr[1]/td[2]"))).click()  # 仓库
        with allure.step("选择仓库（指定默认仓库）"):
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[text()='默认仓库']"))).click()  # 选择仓库（指定默认仓库）
        with allure.step("商品"):
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[@id='app']/div/div[2]/section/div[2]/form/div[2]/div/div/div/div/div[2]/div[2]/div/div[2]/table/tbody/tr[1]/td[3]"))).click()  # 商品
        with allure.step("选择商品（指定0400001商品）"):
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[text()='0400001']"))).click()      #选择商品（指定0400001商品）
        with allure.step("商品数量"):
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[@id='app']/div/div[2]/section/div[2]/form/div[2]/div/div/div/div/div[2]/div[1]/div[2]/table/tbody/tr[1]/td[6]"))).click()  # 商品数量
        with allure.step("填写数量"):
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[@id='app']/div/div[2]/section/div[2]/form/div[2]/div/div/div/div/div[2]/div[1]/div[2]/table/tbody/tr[1]/td[6]/div/span/div/input"))).clear()   #清空0
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[@id='app']/div/div[2]/section/div[2]/form/div[2]/div/div/div/div/div[2]/div[1]/div[2]/table/tbody/tr[1]/td[6]/div/span/div/input"))).send_keys(random.randint(1,10))   #填写数量
            time.sleep(1)
        with allure.step("保存并新增"):
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[text()='保存并新增']"))).click()    #保存并新增
            time.sleep(1)



    @allure.story("删除销售预定单")
    def test_delete(self,a):
        with allure.step("点击销售"):
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[text()='销售']"))).click()  # 点击销售
            time.sleep(1)
        with allure.step("点击销售预订"):
            WebDriverWait(a, 10, 0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "text")))[1].click()  # 进入销售预订
        with allure.step("进入销售预订页面断言"):
            time.sleep(1)
            a1 = a.find_elements(By.CLASS_NAME, "tags-view-item")[1].text
            assert a1 == "销售预订"

        with allure.step("勾选第一条"):
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"vxe-checkbox--icon")))[2].click()
        with allure.step("点击删除"):
            time.sleep(3)
            WebDriverWait(a,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"el-button")))[15].click()
        with allure.step("确定"):
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"el-button")))[-1].click()



    @allure.story("存货复制")
    def test_copy(self,a):
        with allure.step("点击库存"):
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[text()='库存']"))).click()   #库存
        with allure.step("点击盘点"):
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"text")))[0].click()   #点击盘点
        with allure.step("勾选最后一个"):
            time.sleep(2)
            WebDriverWait(a,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"vxe-checkbox--icon")))[-1].click()  #勾选最后一个
        with allure.step("点击复制"):
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"el-button")))[15].click()   #点击复制
        with allure.step("点击保存"):
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[text()='保存']"))).click()  #点击保存
            time.sleep(1)
            WebDriverWait(a,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"tags-view-item")))[1].click()
            time.sleep(1)

        with allure.step("断言复制"):
            a.refresh()   #刷新
            time.sleep(3)

            # tr=a.find_elements(By.CLASS_NAME,"vxe-body--row")[0]  #第一级
            # td=tr.find_elements(By.TAG_NAME,"td")[4]   #第二层                             #框架不适用层级
            # a1=td.find_element(By.CLASS_NAME,"vxe-cell--label").text
            a1 = a.find_element(By.XPATH,"//*[@id='app']/div/div[2]/section/div[2]/div[1]/div[2]/div/div/div[2]/div[1]/div[2]/table/tbody/tr[1]/td[5]/div/span").text
            assert a1=="未审核"



    @allure.story("已审核")
    def test_examine(self,a):
        with allure.step("点击库存"):
            time.sleep(2)
            WebDriverWait(a, 10, 0.5).until(EC.presence_of_element_located((By.XPATH, "//*[text()='库存']"))).click()  # 库存
        with allure.step("点击盘点"):
            WebDriverWait(a, 10, 0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "text")))[0].click()  # 点击盘点
            time.sleep(2)
        with allure.step("勾选最后一个"):
            WebDriverWait(a,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"vxe-checkbox--icon")))[1].click()   #勾选第一个
            time.sleep(1)
        with allure.step("点击审核"):
            WebDriverWait(a,10,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[@id='app']/div/div[2]/section/div[2]/div[1]/div[1]/div[2]/div/button"))).click()   #点击审核
            time.sleep(1)
        with allure.step("已审核断言"):
            a.refresh()  # 刷新  断言是否已审核
            time.sleep(2)
            tr = a.find_elements(By.CLASS_NAME, "vxe-body--row")[0]  # 第一级
            td = tr.find_elements(By.TAG_NAME, "td")[4]  # 第二层
            a2 = td.find_element(By.CLASS_NAME, "vxe-cell--label").text
            assert a2 == "已审核"
























































